package cn.drenal.capcap.service.impl;

import cn.drenal.capcap.entity.Activity;
import cn.drenal.capcap.entity.Category;
import cn.drenal.capcap.entity.UpcomingActivity;
import cn.drenal.capcap.entity.vo.UpcomingActivityListVO;
import cn.drenal.capcap.entity.vo.forum.ForumUpcomingActivityVO;
import cn.drenal.capcap.mapper.ActivityMapper;
import cn.drenal.capcap.mapper.UpcomingActivityMapper;
import cn.drenal.capcap.service.ActivityService;
import cn.drenal.capcap.service.CategoryService;
import cn.drenal.capcap.service.UpcomingActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 活动预告服务实现类
 */
@Service
@RequiredArgsConstructor
public class UpcomingActivityServiceImpl extends ServiceImpl<UpcomingActivityMapper, UpcomingActivity> implements UpcomingActivityService {

    private final ActivityService activityService;
    private final CategoryService categoryService;
    private final Random random = new Random();
    private final ActivityMapper activityMapper;

    @Override
    public Page<UpcomingActivityListVO> page(Integer current, Integer size, String title, Integer status) {
        Page<UpcomingActivity> page = new Page<>(current, size);
        LambdaQueryWrapper<UpcomingActivity> wrapper = new LambdaQueryWrapper<>();
        
        // 根据标题模糊查询
        wrapper.like(StringUtils.isNotBlank(title), UpcomingActivity::getTitle, title);
        // 按排序和创建时间排序
        wrapper.orderByAsc(UpcomingActivity::getSort);
        wrapper.orderByDesc(UpcomingActivity::getCreateTime);

        page = page(page, wrapper);
        Page<UpcomingActivityListVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        voPage.setRecords(page.getRecords().stream().map(upcomingActivity -> {
            UpcomingActivityListVO vo = new UpcomingActivityListVO();
            BeanUtils.copyProperties(upcomingActivity, vo);
            Activity activity = activityMapper.selectById(upcomingActivity.getActivityId());
            if (activity != null) {
                vo.setActivityName(activity.getName());
            }
            return vo;
        }).toList());
        
        return voPage;
    }

    @Override
    public List<ForumUpcomingActivityVO> getRandomUpcomingActivities() {
        // 获取当前时间之前的活动预告
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<UpcomingActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(UpcomingActivity::getStartTime, now);
        
        // 获取所有符合条件的活动预告
        List<UpcomingActivity> upcomingActivities = list(wrapper);
        
        // 如果活动预告数量小于等于5，直接返回所有
        if (upcomingActivities.size() <= 5) {
            return convertToVOList(upcomingActivities);
        }
        
        // 随机抽取5个活动预告
        List<UpcomingActivity> randomActivities = new ArrayList<>();
        List<Integer> selectedIndices = new ArrayList<>();
        
        while (randomActivities.size() < 5 && selectedIndices.size() < upcomingActivities.size()) {
            int index = random.nextInt(upcomingActivities.size());
            if (!selectedIndices.contains(index)) {
                selectedIndices.add(index);
                randomActivities.add(upcomingActivities.get(index));
            }
        }
        
        return convertToVOList(randomActivities);
    }
    
    /**
     * 将活动预告实体转换为VO列表
     *
     * @param upcomingActivities 活动预告列表
     * @return 活动预告VO列表
     */
    private List<ForumUpcomingActivityVO> convertToVOList(List<UpcomingActivity> upcomingActivities) {
        return upcomingActivities.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 将活动预告实体转换为VO
     *
     * @param upcomingActivity 活动预告
     * @return 活动预告VO
     */
    private ForumUpcomingActivityVO convertToVO(UpcomingActivity upcomingActivity) {
        ForumUpcomingActivityVO vo = new ForumUpcomingActivityVO();
        vo.setId(upcomingActivity.getId());
        vo.setUpcomingActivityName(upcomingActivity.getTitle());
        
        // 获取活动信息
        Activity activity = activityService.getById(upcomingActivity.getActivityId());
        if (activity != null && activity.getCategoryId() != null) {
            // 获取分类信息
            Category category = categoryService.getById(activity.getCategoryId());
            if (category != null) {
                vo.setCategoryName(category.getName());
            }
        }
        
        return vo;
    }
}